package org.jeecg.modules.alarmRecording.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.alarmGroup.entity.AlarmGroup;
import org.jeecg.modules.alarmGroup.service.IAlarmGroupService;
import org.jeecg.modules.alarmRecording.entity.AlarmRecording;
import org.jeecg.modules.alarmRecording.service.IAlarmRecordingService;
import org.jeecg.modules.alarmRound.service.IAlarmRoundService;
import org.jeecg.modules.alarmSettings.service.IAlarmSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 报警记录
 * @Author: jeecg-boot
 * @Date:   2023-10-26
 * @Version: V1.0
 */
@Api(tags="报警记录")
@RestController
@RequestMapping("/alarmRecording/alarmRecording")
@Slf4j
public class AlarmRecordingController extends JeecgController<AlarmRecording, IAlarmRecordingService> {
	@Autowired
	private IAlarmRecordingService alarmRecordingService;
	 @Autowired
	 private IAlarmRoundService alarmRoundService;
	 @Autowired
	 private IAlarmGroupService alarmGroupService;
	 @Autowired
	 private IAlarmSettingsService alarmSettingsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param alarmRecording
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "报警记录-分页列表查询")
	@ApiOperation(value="报警记录-分页列表查询", notes="报警记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AlarmRecording alarmRecording,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AlarmRecording> queryWrapper = QueryGenerator.initQueryWrapper(alarmRecording, req.getParameterMap());
		Page<AlarmRecording> page = new Page<AlarmRecording>(pageNo, pageSize);
		IPage<AlarmRecording> pageList = alarmRecordingService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping("/getAll")
	public Result<?> getAll(){
		LambdaQueryWrapper<AlarmRecording> wrapper = Wrappers.lambdaQuery();
		wrapper.orderByAsc(AlarmRecording::getHandle);
		wrapper.orderByDesc(AlarmRecording::getCreateTime);
		List<AlarmRecording> list = alarmRecordingService.list(wrapper);
		return Result.OK(list);
	}

	@GetMapping("/getCurrentAlarm")
	public Result<?> getCurrentAlarm(){
		LambdaQueryWrapper<AlarmRecording> wrapper = Wrappers.lambdaQuery();
		wrapper.ne(AlarmRecording::getHandle, 2);
		wrapper.orderByAsc(AlarmRecording::getHandle);
		wrapper.orderByDesc(AlarmRecording::getCreateTime);
		List<AlarmRecording> list = alarmRecordingService.list(wrapper);
		return Result.OK(list);
	}

	@GetMapping("/getCountAlarm")
	public Result<?> getCountAlarm(){
		LambdaQueryWrapper<AlarmRecording> wrapper = Wrappers.lambdaQuery();
		wrapper.ne(AlarmRecording::getHandle, 2);
		int count = alarmRecordingService.count(wrapper);
		return Result.OK(count);
	}

	@PostMapping("/handleAlarm")
	public Result<?> handleAlarm(@RequestBody AlarmRecording alarmRecording){
		alarmRecording.setHandle(1);
		alarmRecordingService.updateById(alarmRecording);
		return Result.OK();
	}

	@PostMapping("/finishHandle")
	public Result<?> finishHandle(@RequestBody AlarmRecording alarmRecording){
		alarmRecording.setHandle(2);
		alarmRecordingService.updateById(alarmRecording);
		return Result.OK();
	}

	@ApiOperation(value="报警记录-测试", notes="报警记录-测试")
	@GetMapping("/test")
	public Result<?> test(){
		String round = alarmRoundService.selectCurrentRound();
		AlarmGroup group = alarmGroupService.getByRound(round);
		String names = group.getPeople();
		String[] split = names.split("，");
		List<String> name = Arrays.asList(split);
		alarmSettingsService.sendMessage(name, "2367");
		AlarmRecording alarmRecording = new AlarmRecording();
		alarmRecording.setAddress("配电柜-2").setDept("生产部").setMessage("1111").setName(names).setHandle(0);
		alarmRecordingService.save(alarmRecording);
		return Result.OK();
	}
	
	/**
	 *   添加
	 *
	 * @param alarmRecording
	 * @return
	 */
	@AutoLog(value = "报警记录-添加")
	@ApiOperation(value="报警记录-添加", notes="报警记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AlarmRecording alarmRecording) {
		alarmRecordingService.save(alarmRecording);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param alarmRecording
	 * @return
	 */
	@AutoLog(value = "报警记录-编辑")
	@ApiOperation(value="报警记录-编辑", notes="报警记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AlarmRecording alarmRecording) {
		alarmRecordingService.updateById(alarmRecording);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报警记录-通过id删除")
	@ApiOperation(value="报警记录-通过id删除", notes="报警记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		alarmRecordingService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报警记录-批量删除")
	@ApiOperation(value="报警记录-批量删除", notes="报警记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.alarmRecordingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报警记录-通过id查询")
	@ApiOperation(value="报警记录-通过id查询", notes="报警记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AlarmRecording alarmRecording = alarmRecordingService.getById(id);
		if(alarmRecording==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(alarmRecording);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param alarmRecording
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AlarmRecording alarmRecording) {
        return super.exportXls(request, alarmRecording, AlarmRecording.class, "报警记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AlarmRecording.class);
    }

}
